//
//  MessageService.m
//  homeland
//
//  Created by SunShine on 13-11-5.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MessageService.h"
#import "UserInfoService.h"
#import "MessageItem.h"
#import "MyMessageItem.h"
#import "ComplainEntity.h"
@implementation MessageService

-(void)loadMessage
{
    if (self.dataList) {
        self.dataList = nil;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    } else {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }//PERSONAL_MESSAGE_LIST 改了
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_ALL_MESSAGE_LIST_URL  customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        //NSDictionary *json = [operation responseJSON];
         NSArray *json = [operation responseJSON];
        //转换对象,添加到列表中
        //[self initDataItem:@"MessageItem" withArray:[json objectForKey:@"flList"]];
        [self initDataItem:@"MyMessageItem" withArray:json];
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}
//我的消息列表
-(void)loadMessageList:(void (^)(NSArray *data))success Failure:(void (^)(NSString *msg))failure{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    } else {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }//PERSONAL_MESSAGE_LIST 改了
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_ALL_MESSAGE_LIST_URL  customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];    [op addCompletionHandler:^(MKNetworkOperation *operation) {

        NSArray *json = [operation responseJSON];
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求失败.");
    }];
    [_currentEngine enqueueOperation:op];
}
//查询区长信件详情 NSDictionary
-(void)queryQzxxInfo:(NSString *)keySLIDOrNil withName:(NSString*)keyName Success:(void (^)(NSDictionary*json))success Failure:(void (^)(NSString *msg))failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:keySLIDOrNil forKey:@"SLID"];
    [params setValue:keyName forKey:@"XFUserName"];
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:GetNewXFinfo_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSDictionary *json = [operation responseJSON];
        
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];

}

-(void)loadMessageCount:(void (^)(NSString *count))success
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    } else {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_MESSAGE_LIST customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSDictionary *json = [operation responseJSON];
        //转换对象,添加到列表中
        NSString *count = [json objectForKey:@"xxzs"];
        success(count);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
//        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}


-(void)loadMessageListWithFldm:(NSString *)fldm
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    }
    else
    {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }
    if ([fldm isEqualToString:@"tzgg"]) {
        [self queryMesageWithURL:PERSONAL_MESSAGE_TZGG_LIST Params:params DataItemName:@"TZGGListItem"];
    } else if ([fldm isEqualToString:@"wyjd"]) {
        [self queryMesageWithURL:PERSONAL_MESSAGE_WYJD_LIST Params:params DataItemName:@"WYJDListItem"];
    } else if ([fldm isEqualToString:@"qzxx"]) {
        [self queryMesageWithURL:PERSONAL_MESSAGE_QZXX_LIST Params:params DataItemName:@"QZXXListItem"];
    } else if ([fldm isEqualToString:@"zczx"]) {
        [self queryMesageWithURL:PERSONAL_MESSAGE_ZCZX_LIST Params:params DataItemName:@"ZCZXListItem"];
    } else if ([fldm isEqualToString:@"zwzxwsyy"]) {
        [self queryMesageWithURL:PERSONAL_MESSAGE_WSYY_LIST Params:params DataItemName:@"WSYYListItem"];
    }
    
    
}

-(void)queryMesageWithURL:(NSString *)url Params:(NSDictionary *)params  DataItemName:(NSString *)itemName
{
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:url customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSArray *json = [operation responseJSON];
        //转换对象,添加到列表中
        [self initDataItem:itemName withArray:json];
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}

//取得通知公告详情
-(void)loadNoticeForID:(NSString *)Id  {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:Id forKey:@"id"];
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_MESSAGE_TZGG_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSDictionary *json = [operation responseJSON];
        //转换对象
        TZGGDetailItem *detail = getObjectFromDic([TZGGDetailItem class], json);
        
        if (self.noticeDelegate && [self.noticeDelegate respondsToSelector:@selector(loadNoticeSuccess:)]) {
            NSLog(@"通知公告xx-->> ");
            [self.noticeDelegate loadNoticeSuccess:detail];
        }
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
//        FailBlock(err);
        if (self.noticeDelegate && [self.noticeDelegate respondsToSelector:@selector(loadNoticeFail:)]) {
            [self.noticeDelegate loadNoticeFail:@"获取通知公告失败"];
        }
    }];
    [_currentEngine enqueueOperation:op];
}
//标记为已读
-(void)markMessageReadForType:(NSString *)type AndID:(NSString *)Id
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:type forKey:@"messageType"];
    [params setValue:Id forKey:@"id"];
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_MESSAGE_READ_MARK_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {

    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
    }];
    [_currentEngine enqueueOperation:op];
}
//wyjd
//我要jd详情
-(void)loadMessageWYJD:(NSString *)dataId  {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
 
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:[PERSONAL_MESSAGE_MY_WYJD_URL stringByAppendingString:dataId] customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"GET"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSDictionary *json = [operation responseJSON];
        //转换对象
        //WYJDListItem *detail = getObjectFromDic([WYJDListItem class], json);
        ComplainEntity *detail = getObjectFromDic([ComplainEntity class], json);
        if (self.noticeDelegate && [self.noticeDelegate respondsToSelector:@selector(loadWYJDSuccess:)]) {
            NSLog(@"我要监督-->> ");
            [self.noticeDelegate loadWYJDSuccess:detail];
        }
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //        FailBlock(err);
        if (self.noticeDelegate && [self.noticeDelegate respondsToSelector:@selector(loadNoticeFail:)]) {
            [self.noticeDelegate loadNoticeFail:@"获取我要监督失败"];
        }
    }];
    [_currentEngine enqueueOperation:op];
}

//我的消息列表
-(void)queryMyMessageList:(void (^)(NSArray *data))success Failure:(void (^)(NSString *msg))failure{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"userName"];
    } else {
        [params setValue:getDeviceIdentifier() forKey:@"imeiCode"];//要加入的唯一标示
    }
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName:PERSONAL_ALL_MESSAGE_LIST_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSArray *json = [operation responseJSON];
        //转换对象,添加到列表中
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        failure(@"请求失败.");
        
    }];
    [_currentEngine enqueueOperation:op];
    
}

//查询反馈意见
-(void)getFeelback:(NSString *)dataId Success:(void (^)(NSDictionary *data))success Failure:(void (^)(NSString *msg))failure{
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName: [PERSONAL_MESSAGE_MY_YJFK_URL stringByAppendingString:dataId]customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:nil httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        success(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求失败.");
    }];
    [currentEngine enqueueOperation:op];
}

//删除数据
-(void)deleteMessageById:(NSString *)url Success:(void (^)(Boolean flag))success Failure:(void (^)(NSString *msg))failure{
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName: url customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:nil params:nil httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        
        Boolean isSuccess = (Boolean)[json objectForKey:@"isSuccess"];
        success(isSuccess);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        failure(@"请求失败.");
    }];
    [currentEngine enqueueOperation:op];
}


@end
